﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using SLPropertyGrid;
using SLPropertyGrid.Converters;
using System.Reflection;
using System.Runtime.Serialization;

namespace FormDesigner
{
    public class TextIdValueEditor : ValueEditorBase
    {
        ComboBox combobox;

        public TextIdValueEditor(PropertyGridLabel label, PropertyItem property)
            : base(label, property)
        {
            if (property.PropertyType == typeof(Char))
            {
                if ((char)property.Value == '\0')
                    property.Value = "";
            }

            property.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(property_PropertyChanged);
            property.ValueError += new EventHandler<ExceptionEventArgs>(property_ValueError);


            List<string> labs = Table.GlobalTable.panel.Children.Where(p => p is Control).Select(p => (Control)p).
                Where(p => p.Tag is HtmlLabelModel && ((HtmlLabelModel)p.Tag).AType == LabelType.表单项).Select(p => ((Label)p).Content.ToString().Replace("\r","")).ToList();

            labs.Insert(0, "请选择");
            combobox = new ComboBox();
            labs.ForEach(p => combobox.Items.Add(p));
            combobox.Height = 20;
            if (combobox.Items.Count > 0)
                combobox.SelectedIndex = 0;

            if (null != property.Value)
                combobox.SelectedItem = property.Value.ToString();
            //combobox.IsReadOnly = !this.Property.CanWrite;
            combobox.Foreground = this.Property.CanWrite ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.Gray);
            combobox.BorderThickness = new Thickness(1);
            combobox.Margin = new Thickness(1, 0, 1, 0);
            combobox.Padding = new Thickness(1, 0, 1, 0);
            //combobox.Background = new SolidColorBrush(Colors.Red);
            combobox.VerticalAlignment = System.Windows.VerticalAlignment.Center;

            if (this.Property.CanWrite)
            {
                combobox.SelectionChanged += combobox_SelectionChanged;
                //combobox.TextChanged += new TextChangedEventHandler(Control_TextChanged);
            }

            this.Content = combobox;
            this.GotFocus += new RoutedEventHandler(StringValueEditor_GotFocus);
        }

        void combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.Property.CanWrite)
                this.Property.Value = combobox.SelectedItem.ToString();
        }

        void property_ValueError(object sender, ExceptionEventArgs e)
        {
            MessageBox.Show(e.EventException.Message);
        }
        void property_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Value")
            {
                if (null != this.Property.Value)
                {
                    combobox.SelectedItem = this.Property.Value.ToString();
                    //combobox.Text = this.Property.Value.ToString();
                }
                else
                {
                    combobox.SelectedIndex = 0;
                    //combobox.Text = string.Empty;
                }
            }

            if (e.PropertyName == "CanWrite")
            {
                if (!this.Property.CanWrite)
                {
                    combobox.SelectionChanged -= combobox_SelectionChanged;
                    //combobox.TextChanged -= new TextChangedEventHandler(Control_TextChanged);
                }
                else
                {
                    combobox.SelectionChanged += combobox_SelectionChanged;
                    //txt.TextChanged += new TextChangedEventHandler(Control_TextChanged);
                }
                //combobox.IsReadOnly = !this.Property.CanWrite;
                combobox.Foreground = this.Property.CanWrite ? new SolidColorBrush(Colors.Black) : new SolidColorBrush(Colors.Gray);
            }
        }

        void StringValueEditor_GotFocus(object sender, RoutedEventArgs e)
        {
            this.combobox.Focus();
        }

        void Control_TextChanged(object sender, TextChangedEventArgs e)
        {
            //if (this.Property.CanWrite)
            //    this.Property.Value = combobox.Text;
        }
    }
}
